/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年8月19日
 */
package com.massyframework.beanskin.sample.plugin;

import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;

/**
 * @author huangkh
 *
 */
public class ByteCodeClassTransformer implements ClassFileTransformer {

	private static final byte[] MAGIC_NUMBER = new byte[]{(byte) 0x20, (byte)0x10, (byte)0x03, (byte)0x12};
	/**
	 * 
	 */
	public ByteCodeClassTransformer() {
	}

	@Override
	public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
			ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
		if (className.equals("com/massyframework/beanskin/sample/fragment/InstrumentTest")) {
			System.out.println("found:"  + className);
		}

		return null;
	}
	
	/**
	 * 判断头部4字节是否为魔数
	 * @param bytes {@link byte[]}
	 * @return {@link boolean}, 返回true表示是，否则返回{@code false}
	 */
	protected boolean isMagicNumber(byte[] bytes) {
		for (int i=0; i<4; i++) {
			if (MAGIC_NUMBER[i] != bytes[i]) {
				return false;
			}
		}
		
		return true;
	}

}
